﻿#include "AncientBringer.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../../MirEnvir/Map.h"
#include "../DelayedAction.h"
#include "../MapObject.h"
#include "../../Settings.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    unsigned char AncientBringer::getAttackRange() const
    {
        return 12;
    }

    AncientBringer::AncientBringer(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool AncientBringer::InAttackRange()
    {
        return getCurrentMap() == getTarget()->getCurrentMap() && Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), getAttackRange());
    }

    void AncientBringer::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;
        ShockTime = 0;

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 2);

        if (!ranged)
        {
            if (getEnvir()->Random->Next(5) > 0)
            {
                S::ObjectAttack *tempVar = new S::ObjectAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                Broadcast(tempVar);

                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);

                PoisonLineAttack(2, damage, DefenceType::ACAgility);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                tempVar2->ObjectID = ObjectID;
                tempVar2->Direction = getDirection();
                tempVar2->Location = getCurrentLocation();
                tempVar2->Type = 1;
                Broadcast(tempVar2);

                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC] * 2);

                PoisonLineAttack(2, damage, DefenceType::ACAgility, true);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
        }
        else
        {
            if (getEnvir()->Random->Next(10) > 0)
            {
                S::ObjectRangeAttack *tempVar3 = new S::ObjectRangeAttack();
                tempVar3->ObjectID = ObjectID;
                tempVar3->Direction = getDirection();
                tempVar3->Location = getCurrentLocation();
                tempVar3->TargetID = getTarget()->ObjectID;
                Broadcast(tempVar3);

                int damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC]);

                int delay = Functions::MaxDistance(getCurrentLocation(), getTarget()->getCurrentLocation()) * 50 + 500; //50 MS per Step

                DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + delay, {getTarget(), damage, DefenceType::ACAgility, static_cast<unsigned char>(4)});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectRangeAttack *tempVar4 = new S::ObjectRangeAttack();
                tempVar4->ObjectID = ObjectID;
                tempVar4->Direction = getDirection();
                tempVar4->Location = getCurrentLocation();
                tempVar4->TargetID = getTarget()->ObjectID;
                tempVar4->Type = 1;
                Broadcast(tempVar4);

                int damage = GetAttackPower(Stats[Stat::MinMC], Stats[Stat::MaxMC] * 2);

                DelayedAction *action = new DelayedAction(DelayedType::RangeDamage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::ACAgility, static_cast<unsigned char>(5)});
                ActionList.push_back(action);

                if (damage > 0)
                {
                    SpawnSlaves();
                }

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar4' statement was not added since tempVar4 was passed to a method or constructor. Handle memory management manually.
            }
        }
    }

    void AncientBringer::CompleteRangeAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);
        unsigned char range = std::any_cast<unsigned char>(data[3]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        auto targets = FindAllTargets(range, target->getCurrentLocation());

        for (int i = 0; i < targets.size(); i++)
        {
            targets[i]->Attacked(this, damage, defence);
        }
    }

    void AncientBringer::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);
        bool poison = std::any_cast<bool>(data[3]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        auto finalDamage = target->Attacked(this, damage, defence);

        if (finalDamage > 0 && poison)
        {
            PoisonTarget(target, 5, 5, PoisonType::Paralysis, 2000);
        }
    }

    void AncientBringer::PoisonLineAttack(int distance, int additionalDelay, DefenceType defenceType, bool poison)
    {
        int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC]);
        if (damage == 0)
        {
            return;
        }

        for (int i = 1; i <= distance; i++)
        {
            Point *target = Functions::PointMove(getCurrentLocation(), getDirection(), i);

            if (!getCurrentMap()->ValidPoint(target))
            {
                continue;
            }

            Cell *cell = getCurrentMap()->GetCell(target);
            if (cell->Objects.empty())
            {
                continue;
            }

            for (int o = 0; o < cell->Objects.size(); o++)
            {
                MapObject *ob = cell->Objects[o];
                if (ob->getRace() == ObjectType::Monster || ob->getRace() == ObjectType::Player)
                {
                    if (!ob->IsAttackTarget(this))
                    {
                        continue;
                    }

                    DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {ob, damage, defenceType, poison});
                    ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
                }
            }
        }
    }

    void AncientBringer::SpawnSlaves()
    {
        int count = std::min(6, 40 - SlaveList.size());

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        for (int i = 0; i < count; i++)
        {
            MonsterObject *mob = GetMonster(getEnvir()->GetMonsterInfo(Settings::AncientBatName));
            if (mob == nullptr)
            {
                continue;
            }

            if (!mob->Spawn(getCurrentMap(), getTarget()->getCurrentLocation()))
            {
                mob->Spawn(getCurrentMap(), getTarget()->getCurrentLocation());
            }

            mob->setTarget(getTarget());
            mob->ActionTime = getEnvir()->getTime() + 2000;
            SlaveList.push_back(mob);
        }
    }

    void AncientBringer::ProcessTarget()
    {
        if (getTarget() == nullptr)
        {
            return;
        }

        if (InAttackRange() && getCanAttack())
        {
            Attack();
            return;
        }

        if (getEnvir()->getTime() < ShockTime)
        {
            setTarget(nullptr);
            return;
        }

        MoveTo(getTarget()->getCurrentLocation());
    }
}
